const MongoCilent = require('mongodb').MongoClient;
const http = require("http");
const url = 'mongodb://localhost:27017';
const dbName = 'testmgdb';
const collName = 'userinfo';
async function getConnect() {
    try {
        let connect = await MongoCilent.connect(url, { useNewUrlParser: true });
        return connect;
    } catch (err) {
        throw err;
    }
}
async function getBookInfo() {
    let connect = await getConnect();
    let coll = connect.db(dbName).collection(collName);
    let result = await coll.find({}).toArray();
    connect.close();
    return result;
}
getBookInfo().then(data => {
    const str = JSON.stringify(data, null, 4);
    const Httpserver = http.createServer(function(req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain;charset=UTF-8' });
        res.end(str);
    })
    Httpserver.listen(3000, function(res, req) {
        console.log("服务器正在3000端口上监听")
    })
}).catch(error => {
    console.log(error);
});